package com.algorithm;

/**
 * 合并2个有序链表
 */
public class MergeTwoSortedLists {

    public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode head=new ListNode();
        ListNode curr=head;
        while(l1!=null&&l2!=null){
            if (l1.val<l2.val){
                head.next=l1;
                l1=l1.next;
            }else {
                head.next=l2;
                l2=l2.next;
            }
            head=head.next;
        }
        if (l1!=null){
            head.next=l1;
        }
        if (l2!=null){
            head.next=l2;
        }
        return curr.next;
    }

    public static void main(String[] args) {
        ListNode l1=new ListNode(1,new ListNode(3,new ListNode(5)));
        ListNode l2=new ListNode(2,new ListNode(4,new ListNode(6)));
        ListNode listNode = mergeTwoLists(l1, l2);
        System.out.println(listNode);
    }

}
